package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.XhContractEXP;
import com.tjsiqi.param.XhContractParam;
import com.tjsiqi.service.SysXhContractService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by lxc
 */

@Controller
@RequestMapping("/sys/XhContract")
public class SysXhContractController {

    @Resource
    private SysXhContractService sysXhContractService;
//  跳转合同备案
    @RequestMapping("/XhContract.page")
    public ModelAndView XhContractPage() {
        return new ModelAndView("XhContract");
    }

//  查询合同备案
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(XhContractParam staffCheckInParam, PageQuery page) {
        System.out.println(staffCheckInParam);
        return JsonData.success(sysXhContractService.searchPageList(staffCheckInParam, page));
    }
// 导出合同备案
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, XhContractParam staffCheckInParam) throws IOException {
        String filename = "合同备案" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<XhContractEXP> util = new ExportExcelUtil<XhContractEXP>();
        List<XhContractEXP> XhContractList = sysXhContractService.exportByParam(staffCheckInParam);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"合同编号", "施工单位", "建设单位", "监理单位", "工程名称","工程描述", "签订时间","状态", "业务员", "录入时间"};
        util.exportExcel("合同备案列表", columnNames, XhContractList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }


}
